<?php

    if(isset($_GET['srcimage']))
    {
        if(!BorderedImage($_GET['srcimage']))
        {
            // return no image
        }
    }
    
    function BorderedImage($sourcefile)
    {

        $sourcefile = $_SERVER['DOCUMENT_ROOT'].$sourcefile;
        $destfile = dirname($sourcefile)."/brd_".basename($sourcefile);
        $srcbrdfile = str_replace($_SERVER['DOCUMENT_ROOT'], "", $destfile);

        if (!file_exists($sourcefile)) return false;
        $size = getimagesize($sourcefile);
        if ($size === false) return false;
        $format = strtolower(substr($size['mime'], strpos($size['mime'], '/')+1));
        
        if(file_exists($destfile))
        {
            header("Content-type: image/$format");
            echo file_get_contents($destfile);
            return true;
        }

        $quality = 90;
        $icfunc = "imagecreatefrom" . $format;
        $isavefunc = "image" . $format;

        $sourceimage = $icfunc($sourcefile);

        $width = imagesx($sourceimage);
        $height = imagesy($sourceimage);

        $border = 0;
        $s[] = Array();
        $i[] = Array();
        $w[] = Array();
        $h[] = Array();

        $borderdir = $_SERVER['DOCUMENT_ROOT'].'/mikron/images/';
        $cr = Array();
        $cr[1] = imagecreatefrompng($borderdir."cr1.png");
        $cr[2] = imagecreatefrompng($borderdir."cr2.png");
        $cr[3] = imagecreatefrompng($borderdir."cr3.png");
        $cr[4] = imagecreatefrompng($borderdir."cr4.png");
        $cr[6] = imagecreatefrompng($borderdir."cr6.png");
        $cr[7] = imagecreatefrompng($borderdir."cr7.png");
        $cr[8] = imagecreatefrompng($borderdir."cr8.png");
        $cr[9] = imagecreatefrompng($borderdir."cr9.png");

        foreach($cr as $key => $img)
        {
            $w[$key] = imagesx($img);
            $h[$key] = imagesy($img);
        }

        $border = $w[1];
        $width2 = $width + $border * 2;
        $height2 = $height + $border * 2;
        $image = imagecreatetruecolor($width2, $height2);

        imagecopyresized($image, $cr[2], 0, 0, 0, 0, $width2, $border, $w[2], $border);
        imagecopyresized($image, $cr[8], 0, $height2 - $border, 0, 0, $width2, $border, $w[8], $border);
        imagecopyresized($image, $cr[4], 0, 0, 0, 0, $border, $height2, $border, $h[4]);
        imagecopyresized($image, $cr[6], $width2 - $border, 0, 0, 0, $border, $height2, $border, $h[6]);
        imagecopyresized($image, $cr[1], 0, 0, 0, 0, $border, $border, $border, $border);
        imagecopyresized($image, $cr[3], $width2 - $border, 0, 0, 0, $border, $border, $border, $border);
        imagecopyresized($image, $cr[7], 0, $height2 - $border, 0, 0, $border, $border, $border, $border);
        imagecopyresized($image, $cr[9], $width2 - $border, $height2 - $border, 0, 0, $border, $border, $border, $border);
        imagecopyresized($image, $sourceimage, $border, $border, 0, 0, $width, $height, $width, $height);

        header("Content-type: image/$format");

        if($format == "jpeg")
        {
            $isavefunc($image, $destfile, $quality);
        }
        else
        {
            $isavefunc($image, $destfile);
        }

        imagedestroy($image);
        echo file_get_contents($destfile);
        return true;

    }

 ?>